/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.view;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.Spacer;
import javax.microedition.lcdui.StringItem;
import javax.microedition.location.AddressInfo;

import com.aroundj2me.controller.ApplicationController;
import com.aroundj2me.controller.ScreenController;
import com.aroundj2me.model.Place;
import com.aroundj2me.uicomponents.HLine;
import com.aroundj2me.uicomponents.ImageButton;
import com.aroundj2me.utility.ImageLoader;
import com.aroundj2me.utility.StringUtils;
/**
 * View for displaying the content of a Place
 *
 */
public class PlaceView extends Form implements CommandListener, ItemCommandListener, IView {
	private ApplicationController controller = null;
	private Place place = null;
	private ImageButton mapButton = null;
	
	//commands
	private Command mapCommand = null;
	
	public PlaceView(){
		super("Place Viewer");
		
		mapButton = new ImageButton("show map", ImageLoader.getImage(ImageLoader.MAP));
		
		createCommands();
		this.setCommandListener(this);
	}
	
	public PlaceView(ApplicationController controller) {
		this();
		this.controller = controller;
	}

	/**
	 * Creates the commands
	 *
	 */
	private void createCommands() {
		mapCommand = new Command("show map", "show on map", Command.OK, 0);
		
		mapButton.setItemCommandListener(this);
		mapButton.setDefaultCommand(mapCommand);
	}
	
	/**
	 * loads the place-data into the UI components
	 * for displaying to the user
	 *
	 */
	public void displayPlace(){
		//remove all components
		reset();
		
		StringItem name = new StringItem(this.place.getLandmark().getName(),"");
		name.setLayout(StringItem.LAYOUT_LEFT | StringItem.LAYOUT_NEWLINE_AFTER);
		
		String[] splitted = StringUtils.split(String.valueOf(place.getDistance()), '.');
		
		StringItem distance =new StringItem("Distance (m): ", splitted[0]);
		AddressInfo addrInfo = this.place.getLandmark().getAddressInfo();
		StringItem street = new StringItem("Street: ", addrInfo.getField(AddressInfo.STREET));
		StringItem city = new StringItem("City: ", addrInfo.getField(AddressInfo.POSTAL_CODE) + " " + addrInfo.getField(AddressInfo.CITY));
		StringItem state = new StringItem("Country: ", addrInfo.getField(AddressInfo.COUNTRY));
		StringItem phone = new StringItem("Phone: ", addrInfo.getField(AddressInfo.PHONE_NUMBER));
		StringItem url = new StringItem("Website: ", addrInfo.getField(AddressInfo.URL));
		
		StringItem categories = new StringItem("Associated categories: ", this.place.getLandmark().getDescription());
		
		this.append(name);
		Spacer spacer = new Spacer(this.getWidth(), 1);
		this.append(spacer);
		this.append(distance);
		this.append(street);
		this.append(city);
		this.append(state);
		this.append(phone);
		this.append(url);
		this.append(categories);
		this.append(new HLine(this.getWidth()));
		this.append(mapButton);
	}
	
	/**
	 * Action handler
	 */
	public void commandAction(Command c, Displayable arg1) {
		if(c.getCommandType() == Command.BACK){
			ScreenController.getInstance().last();
		}
	}
	
	public void commandAction(Command c, Item item) {
		if(c == mapCommand && item == mapButton){
			controller.executeGoogleMapsRequest(true);
		}
	}
	
	/**
	 * Resets the object in order to reuse them without having the
	 * need to instantiate a new one
	 *
	 */
	public void reset(){
		this.deleteAll();
	}
	
	/*
	 * Getters and setters
	 */
	public Place getPlace() {
		return place;
	}

	public void setPlace(Place place) {
		this.place = place;
	}

	public void setController(ApplicationController controller) {
		this.controller = controller;		
	}

}